const Service = require('egg').Service;

class callHistoryService extends Service {
  constructor(ctx) {
    super(ctx);
    this.model = ctx.model.CallHistory;
  }

  async getHistoryList(param) {
    try {
      const option = {
        where: { account_id: param.account_id },
        limit: param.size,
        offset: (param.page - 1) * param.size,
        order: [['create_time', 'desc']]
      };
      console.log('----',option)
      const { rows, count } = await this.model.findAndCountAll(option);
      return { data: { list: rows, total: count } };
    } catch (e) {
      return { error: e.message };
    }
  }

  async addHistory(param) {
    const row = {
      account_id: param.account_id,
      stu_id: param.stu_id,
      stu_name: param.stu_name,
      class_name: param.class_name,
      is_absent: param.is_absent
    };
    try {
      return await this.model.create(row);
    } catch (e) {
      return { error: e.message };
    }
  }

  async updateHistory(param) {
    const option = {
      where: { history_id: param.history_id }
    };
    const row = {
      is_absent: param.is_absent
    }
    try {
      return await this.model.update(row, option);
    } catch (e) {
      return { error: e.message };
    }
  }

  async delHistory(param) {
    const option = {
      where: { 
        history_id: {
          [this.ctx.app.Sequelize.Op.in]: param.history_id_list
        }
      }
    };
    try {
      return await this.model.destroy(option);
    } catch (e) {
      return { error: e.message };
    }
  }

  async delAllHistory(param) {
    const option = {
      where: {
        account_id: param.account_id
       },
    };
    try {
      return await this.model.destroy(option);
    } catch (e) {
      return { error: e.message };
    }
  }
}

module.exports = callHistoryService;
